{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 波士顿房价预测案例——线性回归分析\n",
    "\n",
    "在这个案例中，我们将利用波士顿郊区的房屋信息数据训练和测试一个模型，并对模型的性能和预测能力进行测试。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、导入必要的工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 矩阵操作\n",
    "import pandas as pd # SQL数据处理\n",
    "\n",
    "from sklearn.metrics import r2_score  #评价回归预测模型的性能\n",
    "\n",
    "import matplotlib.pyplot as plt   #画图\n",
    "import seaborn as sns\n",
    "\n",
    "# 图形出现在Notebook里而不是新窗口\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 读取数据\n",
    "做完特征工程后的数据，请先运行2_FE_BostonHousePrice.ipynb，得到文件FE_boston_housing.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CRIM</th>\n",
       "      <th>ZN</th>\n",
       "      <th>INDUS</th>\n",
       "      <th>CHAS</th>\n",
       "      <th>NOX</th>\n",
       "      <th>RM</th>\n",
       "      <th>AGE</th>\n",
       "      <th>DIS</th>\n",
       "      <th>TAX</th>\n",
       "      <th>PTRATIO</th>\n",
       "      <th>...</th>\n",
       "      <th>RAD_2</th>\n",
       "      <th>RAD_3</th>\n",
       "      <th>RAD_4</th>\n",
       "      <th>RAD_5</th>\n",
       "      <th>RAD_6</th>\n",
       "      <th>RAD_7</th>\n",
       "      <th>RAD_8</th>\n",
       "      <th>RAD_24</th>\n",
       "      <th>MEDV</th>\n",
       "      <th>log_MEDV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.18</td>\n",
       "      <td>0.067815</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.314815</td>\n",
       "      <td>0.577505</td>\n",
       "      <td>0.641607</td>\n",
       "      <td>0.269203</td>\n",
       "      <td>0.208015</td>\n",
       "      <td>0.3</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>3.218876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.242302</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.172840</td>\n",
       "      <td>0.547998</td>\n",
       "      <td>0.782698</td>\n",
       "      <td>0.348962</td>\n",
       "      <td>0.104962</td>\n",
       "      <td>0.5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>21.6</td>\n",
       "      <td>3.117950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.242302</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.172840</td>\n",
       "      <td>0.694386</td>\n",
       "      <td>0.599382</td>\n",
       "      <td>0.348962</td>\n",
       "      <td>0.104962</td>\n",
       "      <td>0.5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>34.7</td>\n",
       "      <td>3.575151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000293</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.063050</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.150206</td>\n",
       "      <td>0.658555</td>\n",
       "      <td>0.441813</td>\n",
       "      <td>0.448545</td>\n",
       "      <td>0.066794</td>\n",
       "      <td>0.6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>33.4</td>\n",
       "      <td>3.538057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000705</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.063050</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.150206</td>\n",
       "      <td>0.687105</td>\n",
       "      <td>0.528321</td>\n",
       "      <td>0.448545</td>\n",
       "      <td>0.066794</td>\n",
       "      <td>0.6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>36.2</td>\n",
       "      <td>3.616309</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       CRIM    ZN     INDUS  CHAS       NOX        RM       AGE       DIS  \\\n",
       "0  0.000000  0.18  0.067815   0.0  0.314815  0.577505  0.641607  0.269203   \n",
       "1  0.000236  0.00  0.242302   0.0  0.172840  0.547998  0.782698  0.348962   \n",
       "2  0.000236  0.00  0.242302   0.0  0.172840  0.694386  0.599382  0.348962   \n",
       "3  0.000293  0.00  0.063050   0.0  0.150206  0.658555  0.441813  0.448545   \n",
       "4  0.000705  0.00  0.063050   0.0  0.150206  0.687105  0.528321  0.448545   \n",
       "\n",
       "        TAX  PTRATIO  ...  RAD_2  RAD_3  RAD_4  RAD_5  RAD_6  RAD_7  RAD_8  \\\n",
       "0  0.208015      0.3  ...      0      0      0      0      0      0      0   \n",
       "1  0.104962      0.5  ...      1      0      0      0      0      0      0   \n",
       "2  0.104962      0.5  ...      1      0      0      0      0      0      0   \n",
       "3  0.066794      0.6  ...      0      1      0      0      0      0      0   \n",
       "4  0.066794      0.6  ...      0      1      0      0      0      0      0   \n",
       "\n",
       "   RAD_24  MEDV  log_MEDV  \n",
       "0       0  24.0  3.218876  \n",
       "1       0  21.6  3.117950  \n",
       "2       0  34.7  3.575151  \n",
       "3       0  33.4  3.538057  \n",
       "4       0  36.2  3.616309  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# path to where the data lies\n",
    "#dpath = './data/'\n",
    "df = pd.read_csv(\"FE_boston_housing_minmaxscaler.csv\")\n",
    "\n",
    "#通过观察前5行，了解数据每列（特征）的概况\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  数据基本信息\n",
    "样本数目、特征维数\n",
    "每个特征的类型、空值样本的数目、数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 506 entries, 0 to 505\n",
      "Data columns (total 23 columns):\n",
      "CRIM        506 non-null float64\n",
      "ZN          506 non-null float64\n",
      "INDUS       506 non-null float64\n",
      "CHAS        506 non-null float64\n",
      "NOX         506 non-null float64\n",
      "RM          506 non-null float64\n",
      "AGE         506 non-null float64\n",
      "DIS         506 non-null float64\n",
      "TAX         506 non-null float64\n",
      "PTRATIO     506 non-null float64\n",
      "B           506 non-null float64\n",
      "LSTAT       506 non-null float64\n",
      "RAD_1       506 non-null int64\n",
      "RAD_2       506 non-null int64\n",
      "RAD_3       506 non-null int64\n",
      "RAD_4       506 non-null int64\n",
      "RAD_5       506 non-null int64\n",
      "RAD_6       506 non-null int64\n",
      "RAD_7       506 non-null int64\n",
      "RAD_8       506 non-null int64\n",
      "RAD_24      506 non-null int64\n",
      "MEDV        506 non-null float64\n",
      "log_MEDV    506 non-null float64\n",
      "dtypes: float64(14), int64(9)\n",
      "memory usage: 91.0 KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 从原始数据中分离输入特征x和输出y\n",
    "y = df[\"MEDV\"]\n",
    "\n",
    "X = df.drop([\"MEDV\", \"log_MEDV\"], axis = 1)\n",
    "\n",
    "#特征名称，用于后续显示权重系数对应的特征\n",
    "feat_names = X.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当数据量比较大时，可用train_test_split从训练集中分出一部分做校验集；\n",
    "样本数目较少时，建议用交叉验证。\n",
    "在线性回归中，留一交叉验证有简便计算方式。\n",
    "\n",
    "下面将训练数据分割成训练集和测试集，只是让大家对模型的训练误差、校验集上的测试误差估计、和测试集上的测试误差做个比较。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(404, 21)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将数据分割训练数据与测试数据\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# 随机采样20%的数据构建测试样本，其余作为训练样本\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.2)\n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、确定模型类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 尝试缺省参数的线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>RM</td>\n",
       "      <td>20.356954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ZN</td>\n",
       "      <td>5.815739</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>RAD_24</td>\n",
       "      <td>4.644938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>3.550457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CHAS</td>\n",
       "      <td>2.684704</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>RAD_7</td>\n",
       "      <td>1.694896</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>RAD_8</td>\n",
       "      <td>1.353203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>RAD_3</td>\n",
       "      <td>1.235936</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>INDUS</td>\n",
       "      <td>0.621810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>AGE</td>\n",
       "      <td>-0.055280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>RAD_4</td>\n",
       "      <td>-0.391399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>RAD_5</td>\n",
       "      <td>-0.518265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>RAD_2</td>\n",
       "      <td>-1.843667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>RAD_6</td>\n",
       "      <td>-2.508106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>RAD_1</td>\n",
       "      <td>-3.667536</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>TAX</td>\n",
       "      <td>-5.109656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NOX</td>\n",
       "      <td>-6.819892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>PTRATIO</td>\n",
       "      <td>-8.455428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CRIM</td>\n",
       "      <td>-9.984341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>DIS</td>\n",
       "      <td>-17.381223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>LSTAT</td>\n",
       "      <td>-21.450356</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    columns       coef\n",
       "5        RM  20.356954\n",
       "1        ZN   5.815739\n",
       "20   RAD_24   4.644938\n",
       "10        B   3.550457\n",
       "3      CHAS   2.684704\n",
       "18    RAD_7   1.694896\n",
       "19    RAD_8   1.353203\n",
       "14    RAD_3   1.235936\n",
       "2     INDUS   0.621810\n",
       "6       AGE  -0.055280\n",
       "15    RAD_4  -0.391399\n",
       "16    RAD_5  -0.518265\n",
       "13    RAD_2  -1.843667\n",
       "17    RAD_6  -2.508106\n",
       "12    RAD_1  -3.667536\n",
       "8       TAX  -5.109656\n",
       "4       NOX  -6.819892\n",
       "9   PTRATIO  -8.455428\n",
       "0      CRIM  -9.984341\n",
       "7       DIS -17.381223\n",
       "11    LSTAT -21.450356"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 线性回归\n",
    "#class sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=1)\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "# 1.使用默认配置初始化学习器实例\n",
    "lr = LinearRegression()\n",
    "\n",
    "# 2.用训练数据训练模型参数\n",
    "lr.fit(X_train, y_train)\n",
    "\n",
    "# 3. 用训练好的模型对测试集进行预测\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "\n",
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.1.1 模型评价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 0.6939789810509471\n",
      "The r2 score of LinearRegression on train is 0.7549146436868177\n"
     ]
    }
   ],
   "source": [
    "# 使用r2_score评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "#测试集\n",
    "print( 'The r2 score of LinearRegression on test is', r2_score(y_test, y_test_pred_lr))\n",
    "#训练集\n",
    "print( 'The r2 score of LinearRegression on train is', r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAekAAAFsCAYAAADos0H8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAGy5JREFUeJzt3X2UHXWd5/H3l6RNnDQEAwmGRExAUMJDEmwwWQSjEWE2SPCsCiwP4RDAgzoHdX0C16FldEcXBpWR8cj4AAhKgFHD6vEsToZMhHXANGYQiJiMkDEhhhCEEBTJw3f/uJXYSbqTTvft3F/ffr/OyelbdetWfX+3cvvTv6pf1Y3MRJIklWefRhcgSZK6ZkhLklQoQ1qSpEIZ0pIkFcqQliSpUIa0JEmFMqQ16EXEoxExo9F1NFJEvCsifhsRGyJi6l7c7oaIOLSb5y6MiPvqtJ0nI+Lt9ViXtDcZ0mpqXf1y3vGXf2YelZkLd7OeCRGRETG0n0pttGuBD2Zma2b+Yscnq7a/WIXqqoi4LiKG9HWj1fZ+09f1SM3KkJYKUED4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7mT59OqtWrdo2fc0113D88cdz7LHHctVVVwHw4osvMmvWLCZPnszRRx/NvHnzgFovdustoL/1rW9xxBFH8Ja3vIX7779/2/ouvPBC7rrrrm3Tra2tAGzYsIGZM2dy3HHHccwxxzB//vydalu9ejUnn3wyU6ZM4eijj+anP/1pj9q0cOFCTj/9dADa29u56KKLmDFjBoceeuh24X3rrbdywgknMGXKFN73vvdtC97LLruMtrY2jjrqqG3vAdRueX311Vfz5je/mTvvvLNHtfSUh7slSdvZvHkzCxYsYO7cuQDcc889LFu2jAcffJDM5IwzzmDRokWsXbuWgw8+mB/96EcAPP/889utZ/Xq1Vx11VV0dHQwcuRI3vrWtzJ16tRdbnv48OF8//vfZ7/99uOZZ55h2rRpnHHGGdsNwvrOd77Dqaeeyqc+9Sk2b97MH/7wh16181e/+hX33nsvL7zwAq9//eu57LLLWL58OfPmzeP++++npaWF97///dx2221ccMEFfO5zn2PUqFFs3ryZmTNn8vDDD3Psscduq/u+++7rVR27YkhLkgD44x//yJQpU3jyySd54xvfyCmnnALUQvqee+7ZFrAbNmxg2bJlnHTSSXz0ox/lE5/4BKeffjonnXTSdut74IEHmDFjBqNH177w6ayzzuLXv/71LmvITK688koWLVrEPvvsw6pVq1izZg2vfvWrty1z/PHHc9FFF7Fx40bOPPNMpkyZ0qv2zpo1i2HDhjFs2DDGjBnDmjVrWLBgAR0dHRx//PHb3pMxY8YAcMcdd3DjjTeyadMmVq9ezWOPPbYtpM8666xe1bA7Hu6WJAF/Pie9YsUKXn755W3npDOTK664giVLlrBkyRKWL1/O3LlzOeKII+jo6OCYY47hiiuu4Oqrr95pnd1dhjR06FC2bNmybf0vv/wyALfddhtr166lo6ODJUuWcNBBB+10rfHJJ5/MokWLGDduHOeffz633HJLr9o7bNiwbY+HDBnCpk2byEzmzJmzra2PP/447e3tPPHEE1x77bUsWLCAhx9+mFmzZm1X14gRI3pVw+7Yk9Ye6emI03qPTJW094wcOZLrr7+e2bNnc9lll3Hqqafy6U9/mnPPPZfW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fAi4CNlE7tPHjhhVaJxHxbWqHZRJ4Enjf1nNGA1116cOX+PPtZz/X4JLqKiIOBb5fTQ4FvtMMbYyI7wIzqH3t3xrgKuAHwB3AIcB/Au/JzAE78KqbNs6gST6LEfFm4KfAL6ldLQNwJbVztk2xH3fRxnMoYD82bUhLkjTQDcbD3ZIkDQiGtCRJhTKkJUkqlCEtSVKhDGlJkgplSEuSVChDWpKkQv1/vLmQS9LZgsQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#在训练集上观察预测残差的分布，看是否符合模型假设：噪声为0均值的高斯噪声\n",
    "f, ax = plt.subplots(figsize=(7, 5)) \n",
    "f.tight_layout() \n",
    "ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5); \n",
    "ax.set_title(\"Histogram of Residuals\") \n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "残差分布和高斯分布比较匹配，但还是大值这边的尾巴更长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAARgAAADQCAYAAADcQn7hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJztnXuUVOWV6H+7i0KqMWNDJBFaHo66wAcBxg5iuJNRrgojahgfUQddTuKomWsyarJQvNEAudxIQoze3GRlxsSoNzoCiumAJsEXTm7MVdOkQWyVifgIFC5hAg3atFB07/tHndNUV59z6tTj1KNr/9aqVXVOn6pvQ/XZvV/f3qKqGIZhREFDpQUwDGPwYgrGMIzIMA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hGZJiCMQwjMkzBGIYRGaZgDMOIjHqt5DWyEBE3PQlwDNAD7HKOp6vqwQrItA64RFU/KPfaRmmwNLUxABFZDHyoqt/NOi+kf2d6Pd9YuvXLso4RPeYiGYGIyAki8qqI/Avp3cpjRaQz4+eXi8hPnNefFJHHRaTN6dMyw+Pz/lFEfi4i65x+PLf7rDNaRLZnVOp+QURecXq/3B92PaOymItkhOFk4Auq+qWMfTRefB/4jqq+6Oy8fgI41eO66c75g8DvnWZMH2auA+BuqBSRKcCtwGdUdbeIjMxzPaNCmIIxwrBVVX8f4rqzgYkZO61HiEhCVbuzrlunqnsARKSV9HaEXwesMwtYqaq7AdznPNYzKoQpGCMMXRmve+nfdmBYxmshXEA4O/DnHndlX5jxuV7BwrDrGRXCYjBGXjiB1z0icqKINAB/l/HjZ4Ab3AMRmerzMeeKSJOINAKfA17IsewzwOWua5ThIoVdz6gQpmCMQriVtEvzLOnd3y43ADOdYOxrwLU+7/8t8G+kd0I/oqobgxZT1VeA7wC/cbrXLc9zPaNCWJraKCsi8o/Aqap6U6VlMaLHLBjDMCLDLBjDMCLDLBjDMCLDFIxhGJFhCsYwjMgwBWMYRmSYgjEMIzL+PxDneBRI9kf+AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 288x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#还可以观察预测值与真值的散点图\n",
    "plt.figure(figsize=(4, 3))\n",
    "plt.scatter(y_train, y_train_pred_lr)\n",
    "plt.plot([-3, 3], [-3, 3], '--k')   #数据已经标准化，3倍标准差即可\n",
    "plt.axis('tight')\n",
    "plt.xlabel('True price')\n",
    "plt.ylabel('Predicted price')\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在y的真值大的部分预测效果不好\n",
    "\n",
    "###  <font color=\"red\">Q5. 代码中给出了岭回归（RidgeCV）和Lasso（LassoCV）超参数（alpha_）调优的过程，请结合两个最佳模型以及最小二乘线性回归模型的结果，给出什么场合应该用岭回归，什么场合用Lasso，什么场合用最小二乘。</font>\n",
    "\n",
    "在特征数据相关性强，存在很大冗余时，最小二乘线性回归失效，可使用岭回归、Lasso回归。\n",
    "超参数调优先使用默认参数例如 alphas = [ 0.01, 0.1, 1, 10,100]，如果发现最小值在搜索边界上，再对应扩大搜索范围。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 正则化的线性回归（L2正则 --> 岭回归）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of RidgeCV on test is 0.6961684813070186\n",
      "The r2 score of RidgeCV on train is 0.7548524440445552\n"
     ]
    }
   ],
   "source": [
    "#岭回归／L2正则\n",
    "#class sklearn.linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0), fit_intercept=True, \n",
    "#                                  normalize=False, scoring=None, cv=None, gcv_mode=None, \n",
    "#                                  store_cv_values=False)\n",
    "from sklearn.linear_model import  RidgeCV\n",
    "\n",
    "#1. 设置超参数（正则参数）范围\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "#n_alphas = 20\n",
    "#alphas = np.logspace(-5,2,n_alphas)\n",
    "\n",
    "#2. 生成一个RidgeCV实例\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)  \n",
    "\n",
    "#3. 模型训练\n",
    "ridge.fit(X_train, y_train)    \n",
    "\n",
    "#4. 预测\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    "\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print('The r2 score of RidgeCV on test is', r2_score(y_test, y_test_pred_ridge))\n",
    "print('The r2 score of RidgeCV on train is', r2_score(y_train, y_train_pred_ridge))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEKCAYAAAAfGVI8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3XecVIW5//HPQ0ekSHWlCFIFQ3NFI4mh6bUFNfY0k5hwjTHBX24STfldMSY3N9FYYsuPxESTmNiJiA1pGqOoS++wdFhgl95hd+f5/XHOwLjuLrOwZ8rO9/167WtnTpnzzGGZ7zynmrsjIiK5q166CxARkfRSEIiI5DgFgYhIjlMQiIjkOAWBiEiOUxCIiOQ4BYGISI5TEIiI5DgFgYhIjmuQ7gKS0bZtW+/atWu6yxARySqzZs3a6u7tjjVdVgRB165dKSgoSHcZIiJZxczWJjOdNg2JiOQ4BYGISI5TEIiI5DgFgYhIjlMQiIjkOAWBiEiOUxCIiOQ4BYGISAY6WFrOuImL2LBjf+TLUhCIiGSgpz9Yx5PvrWHDjgORL0tBICKSYQ6WlvP7t1fy6TPacN4ZbSJfnoJARCTD/P2DdZTsOcTYUT1TsjwFgYhIBjlYWs7jb6/kvDNap6QbAAWBiEhG+ceHYTcwslfKlqkgEBHJEAdLy3l8RtANfLp7aroBiDgIzGyNmS0ws7lmVhAOG2dmG8Nhc83s0ihrEBHJFv/4cB3FKe4GIDX3Ixju7lsrDHvA3e9LwbJFRLJCvBs4t1tquwHQpiERkYzwTNgN3D4qtd0ARB8EDkw2s1lmNiZh+G1mNt/M/mRmp0Rcg4hIRosfKTQkDd0ARB8EQ919MHAJ8B0zuwB4HOgODAQ2Ab+tbEYzG2NmBWZWUFJSEnGZIiLp8+xH69my+xC3p+i8gYoiDQJ3Lwp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1WkZrLyonMix+EzBFfhLAe2mNnbwDnh8OfdPQZsNrPpCfPkASUJz68LLw3eIBzXl+CyAIliwLPh478BiRcQiz+eRRA+AA2BR8xsIFAO9EqYvpjgUh8ikVIQSK6o6iYx1d085gDBNYQws27AD4Bz3H2HmT0ZH3cMiddwORT+Lufo/73/Q3CNpwEEHfrBhOmbhDWIREqbhiRXvANcb2b1w+32FxBcHO5dghu31DOzDgS3KoxbAvQIH7cA9gG7wukuqWI59QiuPgrwxfD1q9MS2BR2JF8huP1kXC8y4+qzUsepI5BcMYFg+/88gm/pP3L3zWb2IjCS4AN3OcGdoXaF87xKEAxT3H2emc0huDLlKuDfVSxnH9DPzGaFr3P9Mep6DHjRzK4luDrovoRxw8MaRCKlq49KzjOzkz24I1Ubgi5haBgSTQk+nIeG+xaSea297n5yLdX1DnCFu++ojdcuFMUjAAAAVElEQVQTqYo6AhGYZGatgEbAPe6+GcDdD5jZXQT3hl2XyoLCzVf3KwQkFdQRiIjkOO0sFhHJcQoCEZEcpyAQEclxCgIRkRynIBARyXEKAhGRHPf/AUiZ4DcVx3cBAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 0.1\n"
     ]
    }
   ],
   "source": [
    "mse_mean = np.mean(ridge.cv_values_, axis = 0)\n",
    "plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas),1)) \n",
    "\n",
    "#这是为了标出最佳参数的位置，不是必须\n",
    "#plt.plot(np.log10(ridge.alpha_)*np.ones(3), [0.28, 0.29, 0.30])\n",
    "\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()\n",
    "\n",
    "print ('alpha is:', ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>RM</td>\n",
       "      <td>20.356954</td>\n",
       "      <td>20.209673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ZN</td>\n",
       "      <td>5.815739</td>\n",
       "      <td>5.668449</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>RAD_24</td>\n",
       "      <td>4.644938</td>\n",
       "      <td>4.495999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>3.550457</td>\n",
       "      <td>3.569715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CHAS</td>\n",
       "      <td>2.684704</td>\n",
       "      <td>2.703375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>RAD_7</td>\n",
       "      <td>1.694896</td>\n",
       "      <td>1.652060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>RAD_8</td>\n",
       "      <td>1.353203</td>\n",
       "      <td>1.369027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>RAD_3</td>\n",
       "      <td>1.235936</td>\n",
       "      <td>1.276021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>INDUS</td>\n",
       "      <td>0.621810</td>\n",
       "      <td>0.517086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>AGE</td>\n",
       "      <td>-0.055280</td>\n",
       "      <td>-0.048454</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>RAD_4</td>\n",
       "      <td>-0.391399</td>\n",
       "      <td>-0.389052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>RAD_5</td>\n",
       "      <td>-0.518265</td>\n",
       "      <td>-0.503234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>RAD_2</td>\n",
       "      <td>-1.843667</td>\n",
       "      <td>-1.756881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>RAD_6</td>\n",
       "      <td>-2.508106</td>\n",
       "      <td>-2.495217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>RAD_1</td>\n",
       "      <td>-3.667536</td>\n",
       "      <td>-3.648724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>TAX</td>\n",
       "      <td>-5.109656</td>\n",
       "      <td>-4.958641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NOX</td>\n",
       "      <td>-6.819892</td>\n",
       "      <td>-6.565940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>PTRATIO</td>\n",
       "      <td>-8.455428</td>\n",
       "      <td>-8.431607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CRIM</td>\n",
       "      <td>-9.984341</td>\n",
       "      <td>-9.567877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>DIS</td>\n",
       "      <td>-17.381223</td>\n",
       "      <td>-16.868997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>LSTAT</td>\n",
       "      <td>-21.450356</td>\n",
       "      <td>-21.357121</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    columns    coef_lr  coef_ridge\n",
       "5        RM  20.356954   20.209673\n",
       "1        ZN   5.815739    5.668449\n",
       "20   RAD_24   4.644938    4.495999\n",
       "10        B   3.550457    3.569715\n",
       "3      CHAS   2.684704    2.703375\n",
       "18    RAD_7   1.694896    1.652060\n",
       "19    RAD_8   1.353203    1.369027\n",
       "14    RAD_3   1.235936    1.276021\n",
       "2     INDUS   0.621810    0.517086\n",
       "6       AGE  -0.055280   -0.048454\n",
       "15    RAD_4  -0.391399   -0.389052\n",
       "16    RAD_5  -0.518265   -0.503234\n",
       "13    RAD_2  -1.843667   -1.756881\n",
       "17    RAD_6  -2.508106   -2.495217\n",
       "12    RAD_1  -3.667536   -3.648724\n",
       "8       TAX  -5.109656   -4.958641\n",
       "4       NOX  -6.819892   -6.565940\n",
       "9   PTRATIO  -8.455428   -8.431607\n",
       "0      CRIM  -9.984341   -9.567877\n",
       "7       DIS -17.381223  -16.868997\n",
       "11    LSTAT -21.450356  -21.357121"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 正则化的线性回归（L1正则 --> Lasso）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.6945099371682091\n",
      "The r2 score of LassoCV on train is 0.7548861511225583\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_split.py:2053: FutureWarning: You should specify a value for 'cv' instead of relying on the default value. The default value will change from 3 to 5 in version 0.22.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "#### Lasso／L1正则\n",
    "# class sklearn.linear_model.LassoCV(eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, \n",
    "#                                    normalize=False, precompute=’auto’, max_iter=1000, \n",
    "#                                    tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=1,\n",
    "#                                    positive=False, random_state=None, selection=’cyclic’)\n",
    "from sklearn.linear_model import LassoCV\n",
    "\n",
    "#1. 设置超参数搜索范围\n",
    "#alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "\n",
    "#2. 生成学习器实例\n",
    "#lasso = LassoCV(alphas=alphas)\n",
    "\n",
    "#1. 设置超参数搜索范围\n",
    "#Lasso可以自动确定最大的alpha，所以另一种设置alpha的方式是设置最小的alpha值（eps） 和 超参数的数目（n_alphas），\n",
    "#然后LassoCV对最小值和最大值之间在log域上均匀取值n_alphas个\n",
    "# np.logspace(np.log10(alpha_max * eps), np.log10(alpha_max),num=n_alphas)[::-1]\n",
    "\n",
    "#2 生成LassoCV实例（默认超参数搜索范围）\n",
    "lasso = LassoCV()  \n",
    "\n",
    "#3. 训练（内含CV）\n",
    "lasso.fit(X_train, y_train)  \n",
    "\n",
    "#4. 测试\n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print( 'The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))\n",
    "print( 'The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAELCAYAAADURYGZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3Xl4VPd97/H3VytoAQlJiEUGhAEDXjBG3pfEC66dzXZiN2mSliZOaZ+btkncPolz3Vs3TXtvnKTN0rTpdZOnpUkab7Gv7Sy2Md6axHYMtjE7YkcIJCEJCUlone/9Y46wTCQYbJ05s3xez6PnLPObOd8zA/OZs/2OuTsiIpK9cqIuQEREoqUgEBHJcgoCEZEspyAQEclyCgIRkSynIBARyXKhBoGZfcbMNprZJjP7bDBvipmtNrP6YFgeZg0iInJyoQWBmZ0D/BFwEbAEeJ+ZzQfuBNa4+3xgTTAtIiIRCXOLYBHwkrv3uPsg8DxwC3ATsCposwq4OcQaRETkFMIMgo3AVWZWYWZFwHuAM4Bqdz8IEAynhliDiIicQl5YL+zuW8zsHmA10AWsBwYTfb6ZrQRWAhQXFy9buHBhKHWKiGSqdevWHXb3qlO1s2T1NWRm/xtoAD4DvNvdD5rZdOA5dz/rZM+tq6vztWvXJqNMEZGMYWbr3L3uVO3CPmtoajCcBXwQ+DHwGLAiaLICeDTMGkRE5ORC2zUU+ImZVQADwKfdvd3MvgI8YGa3A/uA20KuQURETiLUIHD3K0eZ1wpcG+ZyRUQkcbqyWEQkyykIRESynIJARCTLKQhERLKcgkBEJAU1tPdwzxNbaT7aG/qyFAQiIinowbUN/OvzO+kfjIW+LAWBiEiKGYo5D67dz5Xzq6gpLwp9eQoCEZEU89/1LTR29PKRC89IyvIUBCIiKeb+V/YzpbiA6xZVJ2V5CgIRkRRyuKuP1Zub+NAFM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SLLFC8CHzSw32G9/FfFOv34JfCg4VlDNW7s13wLMC8YnAd1AR9DuxjGWk0O8V0mAjwavfzKTgYPBFsnvE7/d4bAFpGdvp5JmtEUg2eIR4vv/1xP/lf55dz9kZj8BriX+hbsdeJn4XaMgfg+MdwNPu/t6M3uNeC+ku4BfjbGcbuBsM1sXvM6HT1HXvwA/MbPbgGeD5w+7OqhBJFTqfVSynpmVuHuXmVUQ30q4PAiJicS/nC8Pji0k8lpd7l4yTnW9ANzk8ftxi4RGWwQi8FMzKwMKgC+7+yEAdz9mZncTv8/svmQWFOy++keFgCSDtghERLKcDhaLiGQ5BYGISJZTEIiIZDkFgYhIllMQiIhkOQWBiEiW+//Cw8uaG/eEWAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 0.0013884165768061959\n"
     ]
    }
   ],
   "source": [
    "mses = np.mean(lasso.mse_path_, axis = 1)\n",
    "plt.plot(np.log10(lasso.alphas_), mses) \n",
    "#plt.plot(np.log10(lasso.alphas_)*np.ones(3), [0.3, 0.4, 1.0])\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()    \n",
    "            \n",
    "print ('alpha is:', lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>RM</td>\n",
       "      <td>20.356954</td>\n",
       "      <td>20.209673</td>\n",
       "      <td>20.349884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ZN</td>\n",
       "      <td>5.815739</td>\n",
       "      <td>5.668449</td>\n",
       "      <td>5.690495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>RAD_24</td>\n",
       "      <td>4.644938</td>\n",
       "      <td>4.495999</td>\n",
       "      <td>4.849636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>3.550457</td>\n",
       "      <td>3.569715</td>\n",
       "      <td>3.527686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CHAS</td>\n",
       "      <td>2.684704</td>\n",
       "      <td>2.703375</td>\n",
       "      <td>2.693696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>RAD_7</td>\n",
       "      <td>1.694896</td>\n",
       "      <td>1.652060</td>\n",
       "      <td>2.018658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>RAD_8</td>\n",
       "      <td>1.353203</td>\n",
       "      <td>1.369027</td>\n",
       "      <td>1.697906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>RAD_3</td>\n",
       "      <td>1.235936</td>\n",
       "      <td>1.276021</td>\n",
       "      <td>1.631360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>INDUS</td>\n",
       "      <td>0.621810</td>\n",
       "      <td>0.517086</td>\n",
       "      <td>0.422357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>AGE</td>\n",
       "      <td>-0.055280</td>\n",
       "      <td>-0.048454</td>\n",
       "      <td>-0.024262</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>RAD_4</td>\n",
       "      <td>-0.391399</td>\n",
       "      <td>-0.389052</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>RAD_5</td>\n",
       "      <td>-0.518265</td>\n",
       "      <td>-0.503234</td>\n",
       "      <td>-0.126658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>RAD_2</td>\n",
       "      <td>-1.843667</td>\n",
       "      <td>-1.756881</td>\n",
       "      <td>-1.365961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>RAD_6</td>\n",
       "      <td>-2.508106</td>\n",
       "      <td>-2.495217</td>\n",
       "      <td>-2.113486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>RAD_1</td>\n",
       "      <td>-3.667536</td>\n",
       "      <td>-3.648724</td>\n",
       "      <td>-3.236791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>TAX</td>\n",
       "      <td>-5.109656</td>\n",
       "      <td>-4.958641</td>\n",
       "      <td>-4.872897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NOX</td>\n",
       "      <td>-6.819892</td>\n",
       "      <td>-6.565940</td>\n",
       "      <td>-6.632717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>PTRATIO</td>\n",
       "      <td>-8.455428</td>\n",
       "      <td>-8.431607</td>\n",
       "      <td>-8.400752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CRIM</td>\n",
       "      <td>-9.984341</td>\n",
       "      <td>-9.567877</td>\n",
       "      <td>-9.708141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>DIS</td>\n",
       "      <td>-17.381223</td>\n",
       "      <td>-16.868997</td>\n",
       "      <td>-17.122870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>LSTAT</td>\n",
       "      <td>-21.450356</td>\n",
       "      <td>-21.357121</td>\n",
       "      <td>-21.473748</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    columns    coef_lr  coef_ridge  coef_lasso\n",
       "5        RM  20.356954   20.209673   20.349884\n",
       "1        ZN   5.815739    5.668449    5.690495\n",
       "20   RAD_24   4.644938    4.495999    4.849636\n",
       "10        B   3.550457    3.569715    3.527686\n",
       "3      CHAS   2.684704    2.703375    2.693696\n",
       "18    RAD_7   1.694896    1.652060    2.018658\n",
       "19    RAD_8   1.353203    1.369027    1.697906\n",
       "14    RAD_3   1.235936    1.276021    1.631360\n",
       "2     INDUS   0.621810    0.517086    0.422357\n",
       "6       AGE  -0.055280   -0.048454   -0.024262\n",
       "15    RAD_4  -0.391399   -0.389052   -0.000000\n",
       "16    RAD_5  -0.518265   -0.503234   -0.126658\n",
       "13    RAD_2  -1.843667   -1.756881   -1.365961\n",
       "17    RAD_6  -2.508106   -2.495217   -2.113486\n",
       "12    RAD_1  -3.667536   -3.648724   -3.236791\n",
       "8       TAX  -5.109656   -4.958641   -4.872897\n",
       "4       NOX  -6.819892   -6.565940   -6.632717\n",
       "9   PTRATIO  -8.455428   -8.431607   -8.400752\n",
       "0      CRIM  -9.984341   -9.567877   -9.708141\n",
       "7       DIS -17.381223  -16.868997  -17.122870\n",
       "11    LSTAT -21.450356  -21.357121  -21.473748"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T)), \"coef_lasso\":list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
